package lmycc.test.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
public class MailService {

    @Autowired
    private JavaMailSender mailSender; //框架自带的

    @GetMapping("/send")
    public String sendSimpleMail(String sendTo, String title, String content) {
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = null; //开启带附件true
        try {
            messageHelper = new MimeMessageHelper(mailMessage, true);
            messageHelper.setFrom("13932460374@163.com");                   //发送者
            messageHelper.setTo("1428180172@qq.com");                       //接收者
            messageHelper.setSubject("SpringBoot MailTest");                //发送的标题
            messageHelper.setText("SpringBoot MailTest");                   //发送的内容
//            FileSystemResource filePath = new FileSystemResource(new File("F:\\q.jpg")); //选择文件
//            messageHelper.addAttachment("图片.jpg", filePath);//加入邮件
            mailSender.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return "success";
    }
}
